package com.algorithm.hqy;

import java.util.HashMap;

/**
 * @Classname EightQueen
 * @Date 2019/9/19 14:08
 * @auther by hqy
 */
public class EightQueen {

    private static int count;

    private static int judgeTime;

    private static int[] queen;

    public static void check(int n) {

        if (n == queen.length) {
            print();
            System.out.println("-------------------------");
            count++;
            return;
        }
        for (int i = 0; i < queen.length; i++) {
            queen[n] = i;

            if (judge(n)) {
                check(n + 1);
            }
        }


    }

    public static boolean judge(int n) {
        judgeTime++;
        for (int i = 0; i < n; i++) {
            if (queen[i] == queen[n] || Math.abs(n - i) == Math.abs(queen[n] - queen[i])) {
                return false;
            }
        }
        return true;

    }

    public static void print() {
        for (int i = 0; i < queen.length; i++) {
            for (int j = 0; j < queen.length; j++) {
                if (queen[i] == j) {
                    System.out.printf(queen[i] + "  ");
                } else {
                    System.out.printf(0 + "  ");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        queen = new int[8];
        check(0);
        System.out.println(judgeTime);
        System.out.println(count);



    }
}
